package com.sam.mvp.helper;

import android.support.v7.util.DiffUtil;

import com.crazysunj.multitypeadapter.helper.AsynAdapterHelper;
import com.sam.R;
import com.sam.base.MutiTypeTitleEntity;
import com.sam.mvp.entity.HomeEnity;
import com.sam.mvp.ui.adapter.HomeAdapter;

import java.util.List;

/**
 * @ProjectName: SamLib
 * @Package: com.sam.mvp.helper
 * @ClassName: HomeAdapterHelper
 * @Description:
 * @Author: Carl
 * @CreateDate: 2018/10/24 11:28
 * @UpdateUser:
 * @UpdateDate: 2018/10/24 11:28
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class HomeAdapterHelper extends AsynAdapterHelper<MutiTypeTitleEntity> {

    public static final int TOP = 1;
    public static final int CENTER = 2;
    public static final int BOTTOM = 3;

    public HomeAdapterHelper() {
        super(null);
    }

    @Override
    protected void registerModule() {
        registerModule(TOP)
                .type(HomeEnity.TOP)
                .layoutResId(R.layout.item_home_top)
                .headerResId(R.layout.header_common)
                .footerResId(R.layout.item_footer)
                .isFolded(true)
                .minSize(4)
                .loading()
                .loadingHeaderResId(R.layout.layout_loading_header)
                .loadingLayoutResId(R.layout.layout_loading)
                .register();

        registerModule(CENTER)
                .type(HomeEnity.CENTER)
                .layoutResId(R.layout.item_home_center)
                .headerResId(R.layout.header_common)
                .loading()
                .loadingHeaderResId(R.layout.layout_loading_header)
                .loadingLayoutResId(R.layout.layout_loading)
                .register();

        registerModule(BOTTOM)
                .type(HomeEnity.BOTTOM)
                .layoutResId(R.layout.item_home_bottom)
                .headerResId(R.layout.header_common)
                .loading()
                .loadingHeaderResId(R.layout.layout_loading_header)
                .loadingLayoutResId(R.layout.layout_loading)
                .register();


    }


    @Override
    protected int getPreDataCount() {
        final HomeAdapter adapter = getBindAdapter();
        return adapter.getHeaderLayoutCount();
    }

    @Override
    protected DiffUtil.Callback getDiffCallBack(List<MutiTypeTitleEntity> oldData, List<MutiTypeTitleEntity> newData) {
        return new TitleDiffCallBack(oldData, newData);
    }

    private static class TitleDiffCallBack extends DiffUtil.Callback {

        private List<MutiTypeTitleEntity> mOldDatas;
        private List<MutiTypeTitleEntity> mNewDatas;

        TitleDiffCallBack(List<MutiTypeTitleEntity> mOldDatas, List<MutiTypeTitleEntity> mNewDatas) {
            this.mOldDatas = mOldDatas;
            this.mNewDatas = mNewDatas;
        }

        @Override
        public int getOldListSize() {
            return mOldDatas == null ? 0 : mOldDatas.size();
        }

        @Override
        public int getNewListSize() {
            return mNewDatas == null ? 0 : mNewDatas.size();
        }

        @Override
        public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {

            MutiTypeTitleEntity oldItem = mOldDatas.get(oldItemPosition);
            MutiTypeTitleEntity newItem = mNewDatas.get(newItemPosition);
            return !(oldItem == null || newItem == null) && oldItem.getItemType() == newItem.getItemType();
        }

        @Override
        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
            MutiTypeTitleEntity oldItem = mOldDatas.get(oldItemPosition);
            MutiTypeTitleEntity newItem = mNewDatas.get(newItemPosition);
            return oldItem.getId() == newItem.getId() && oldItem.getTitle().equals(newItem.getTitle());
        }
    }
}
